package com.youmu.sichuansfc.dao.impl;

import com.youmu.sichuansfc.dao.CompanyRiskDao;
import com.youmu.sichuansfc.domain.YoumuCompanyRisk;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * Created by 杨磊 on 2018/1/23.
 */

@Repository
public class CompanyRiskDaoImpl  implements CompanyRiskDao{


    private EntityManager em;
    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }


    @Override
    public List<YoumuCompanyRisk> findByCompanyId(long companyId) {
        String sql = "from YoumuCompanyRisk where company_id =:companyId and is_delete = 0";

        List<YoumuCompanyRisk> youmuCompanyRiskList = em.createQuery(sql, YoumuCompanyRisk.class)
                .setParameter("companyId", companyId)
                .getResultList();
        return youmuCompanyRiskList;
    }

    @Override
    public YoumuCompanyRisk update(YoumuCompanyRisk youmuCompanyRisk) {
        if(youmuCompanyRisk.getId()!= null){
            em.merge(youmuCompanyRisk);
        }else {
            em.persist(youmuCompanyRisk);
        }
        return youmuCompanyRisk;
    }

    @Override
    public YoumuCompanyRisk findById(long id) {
        String sql = "from YoumuCompanyRisk where id =:id and is_delete = 0";

        List<YoumuCompanyRisk> youmuCompanyRiskList = em.createQuery(sql, YoumuCompanyRisk.class)
                .setParameter("id", id)
                .getResultList();
        if(youmuCompanyRiskList != null && youmuCompanyRiskList.size() > 0){
            return youmuCompanyRiskList.get(0);
        }
        return null;
    }
}
